/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 19/09/2011, 12:53:18 AM
 */

package proyectocatalina;

import java.awt.Color;
import java.net.URISyntaxException;
import javax.swing.Action;

import java.awt.Event;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.DefaultEditorKit;
import modelo.beanusuario;

/**
 *
 * @author clevano
 */
public class Login extends javax.swing.JFrame {
 String texto1="Escriba su Nombre";
 String texto2="Escriba su Contraseña";
      static int idsistema = 1;
    public static beanusuario usuarioglobal;
    public static int  sistema = 1;
    /** Creates new form Login */
    public Login() {
   
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (Exception e) {
                //     If Nimbus is not available, you can set the GUI to another look and feel.
            }
            initComponents();
            this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/huarango.jpg")).getImage());
            //           this.setIconImage (new ImageIcon(getClass().getResource("/Imagenes/iconologo.png")).getImage());
            this.setLocationRelativeTo(null);
            jusuario.setForeground(Color.LIGHT_GRAY);
            jpass.setForeground(Color.LIGHT_GRAY);
            jButton1.requestFocus();
            //        this.requestFocus();
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jEImagePanel1 = new proyectocatalina.JEImagePanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jusuario = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jpass = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inicio de Sesión");
        setBackground(java.awt.SystemColor.activeCaption);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        setUndecorated(true);

        jEImagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/fondo25.jpeg"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/iconman_150.png"))); // NOI18N
        jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setText("SISTEMA INTEGRADO DE MATRICULA - Uso Reservado");

        jButton3.setBackground(java.awt.SystemColor.activeCaption);
        jButton3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton3.setText("-");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(255, 153, 153));
        jButton2.setText("X");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel5.setText("En caso de Perdida o extravio del Password comuniquese con su Administrador");

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 18));
        jLabel1.setText("Usuario :");

        jusuario.setFont(new java.awt.Font("Arial", 0, 12));
        jusuario.setText("Escriba su Nombre");
        jusuario.setToolTipText("<html><font color = #8A0829>Escriba aqui su nombre de usuario</font></html>");
        jusuario.setComponentPopupMenu(jPopupMenu1);
        jusuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jusuarioActionPerformed(evt);
            }
        });
        jusuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jusuarioFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jusuarioFocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Calibri", 1, 18));
        jLabel2.setText("Contraseña :");

        jpass.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jpass.setText("Escriba su Contraseña");
        jpass.setToolTipText("<html><font color = #8A0829>Escriba aqui su clave de acceso</font></html>");
        jpass.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jpassFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jpassFocusLost(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/iconkeys_32.png"))); // NOI18N
        jButton1.setToolTipText("<html><font color = \"red\">Ingresar</font></html>");
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jEImagePanel1Layout = new javax.swing.GroupLayout(jEImagePanel1);
        jEImagePanel1.setLayout(jEImagePanel1Layout);
        jEImagePanel1Layout.setHorizontalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jEImagePanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addGroup(jEImagePanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(10, 10, 10)
                                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jEImagePanel1Layout.createSequentialGroup()
                                        .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jpass, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(10, 10, 10)
                                        .addComponent(jButton1))
                                    .addComponent(jusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jEImagePanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel5)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jEImagePanel1Layout.setVerticalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addGap(35, 35, 35)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jEImagePanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jpass, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1)))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jusuarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jusuarioFocusLost
               // TODO add your handling code here:
        if (jusuario.getText().equalsIgnoreCase("")) {
        jusuario.setText(texto1);
        jusuario.setForeground(Color.LIGHT_GRAY);
        }
    }//GEN-LAST:event_jusuarioFocusLost

        private void jusuarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jusuarioFocusGained
            // TODO add your handling code here:
             this.jPopupMenu1.removeAll();
             //menu copiar
             Action accionCopiar = this.jusuario.getActionMap().get(DefaultEditorKit.copyAction);
             accionCopiar.putValue(Action.NAME, "Copiar");
             accionCopiar.putValue(Action.ACCELERATOR_KEY,KeyStroke.getAWTKeyStroke('C', Event.CTRL_MASK));
             JMenuItem copiar = new JMenuItem(accionCopiar);
             Action accionCortar = this.jusuario.getActionMap().get(DefaultEditorKit.cutAction);
               //menu cortar
            accionCortar.putValue(Action.NAME, "Cortar");
            accionCortar.putValue(
            Action.ACCELERATOR_KEY,
            KeyStroke.getAWTKeyStroke('X', Event.CTRL_MASK));
            JMenuItem cortar = new JMenuItem(accionCortar);
            // Idem para pegar
            Action accionPegar =this.jusuario.getActionMap().get(DefaultEditorKit.pasteAction);
            accionPegar.putValue(Action.NAME, "Pegar");
            accionPegar.putValue(
            Action.ACCELERATOR_KEY,
            KeyStroke.getAWTKeyStroke('V', Event.CTRL_MASK));
            JMenuItem pegar = new JMenuItem(accionPegar);

            this.jPopupMenu1.add(copiar);
            this.jPopupMenu1.add(pegar);
            this.jPopupMenu1.add(cortar);
            if(jusuario.getText().equalsIgnoreCase(texto1)) {
            jusuario.setText("");
            jusuario.setForeground(Color.black);
            }
        }//GEN-LAST:event_jusuarioFocusGained

            private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            abrirlogin();
            }//GEN-LAST:event_jButton1ActionPerformed

            private void jpassFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jpassFocusGained
                char[] pass = jpass.getPassword();
                String password = "";
                for(int i=0;i<=pass.length-1;i++){
                    password = password + pass[i];
                }

                if(password.equalsIgnoreCase(texto2)) {
            jpass.setText("");
            jpass.setForeground(Color.black);
            }
            }//GEN-LAST:event_jpassFocusGained

            private void jpassFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jpassFocusLost
              char[] pass = jpass.getPassword();
                String password = "";
                for(int i=0;i<=pass.length-1;i++){
                    password = password + pass[i];
                }
                if (password.equalsIgnoreCase("")) {
        jpass.setText(texto2);
        jpass.setForeground(Color.LIGHT_GRAY);
        }
            }//GEN-LAST:event_jpassFocusLost

            private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
              this.dispose();
              
            }//GEN-LAST:event_jButton2ActionPerformed

            private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
            this.setState(JFrame.ICONIFIED );
            }//GEN-LAST:event_jButton3ActionPerformed

            private void jusuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jusuarioActionPerformed
                // TODO add your handling code here:
            }//GEN-LAST:event_jusuarioActionPerformed
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private proyectocatalina.JEImagePanel jEImagePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPasswordField jpass;
    private javax.swing.JTextField jusuario;
    // End of variables declaration//GEN-END:variables
public void abrirlogin(){
        char[] pass = jpass.getPassword();
        String password = "";
           for(int i=0;i<=pass.length-1;i++){
                    password = password + pass[i];
                }
        LogicadeNegocio.Login Logueo = new LogicadeNegocio.Login(sistema,jusuario.getText(),password);
        if(Logueo.consultarusuario().equals("abrir")){
            usuarioglobal = Logueo.retornar();
          try {
            Principal princ = null;
            princ = new Principal();
            this.setVisible(false);
            princ.setVisible(true);           
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        }else{
         JOptionPane.showMessageDialog(null,"Debe Ingresar un Usuario/Contraseña Valido");
        }



}
}
